package com.movie.ticket.sale.bossassembly.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.movie.ticket.sale.bossassembly.common.MovieTicket;
import com.movie.ticket.sale.bossassembly.common.UpdateUtil;
import com.movie.ticket.sale.bossassembly.entity.*;
import com.movie.ticket.sale.bossassembly.mapper.*;
import com.movie.ticket.sale.bossassembly.pram.*;
import com.movie.ticket.sale.bossassembly.service.BossAcountService;
import com.movie.ticket.sale.feignClients.bossFeigns;
import com.movie.ticket.sale.moviecommon.common.Result;
import com.movie.ticket.sale.pram.DiscountPram;
import com.movie.ticket.sale.pram.MoviePram;
import com.movie.ticket.sale.pram.PersonPram;
import feign.Param;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author muzi
 * @since 2020-12-09
 */
@RestController
@Slf4j
public class BossAcountController implements bossFeigns {

    @Autowired
    private CinemaMapper cinemaMapper;
    @Autowired
    private BossAcountMapper bossAcountMapper;
    @Autowired
    private BossAcountService bossAcountService;
    @Autowired
    private CinemaContractMapper cinemaContractMapper;
    @Autowired
    private DiscountMapper discountMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private MovieMapper movieMapper;
    @Autowired
    private MovieDataMapper movieDataMapper;
    @Autowired
    private PersonMapper personMapper;
    @Autowired
    private MoviePersonMapper moviePersonMapper;
    @Autowired
    private PersonDataMapper personDataMapper;
    @Autowired
    private MovieTicket movieTicket;
    @Autowired
    private UpdateUtil updateUtil;




    /**
     *作者:木子
     *方法：查看所有的影院
     *参数：
     *返回值:影院列表
     */
    public Result getCinemas(@RequestParam(defaultValue = "1")int currPage,@RequestParam(defaultValue = "5")int number){
        Page<Cinema> page = new Page<>(currPage,number);
        IPage<Cinema> cinemas = cinemaMapper.findCinemas(page);
        return Result.success(cinemas);
    }
    /**
     *作者:木子
     *方法：展示所有电影订单
     *参数：
     *返回值: 电影订单列表
     */
    public Result getOrders(@RequestParam(defaultValue = "1")int currPage,@RequestParam(defaultValue = "5")int number){
        Page<MovieOrder> page = new Page<>(currPage,number);
        IPage<MovieOrder> orders = cinemaMapper.findOrders(page);
        return Result.success(orders);
    }
    /**
     *作者:木子
     *方法：展示用户
     *参数：
     *返回值: 用户列表
     */
    public Result getUser(@RequestParam(defaultValue = "1")int currPage,@RequestParam(defaultValue = "5")int number){
        Page<Object> page = new Page<>(currPage,number);
        IPage<User> users = bossAcountMapper.findUsers(page);
        return Result.success(users);
    }
    /**
     *作者:木子
     *方法：
     *参数：
     *返回值:
     */


    public Result isVip(){
        //查询用户会员占所有用户的比例
        UserVipPram vip = bossAcountService.isVip();
        return Result.success(vip);
    }

    /**
     *作者:木子
     *方法： 审核合同：同意修改     -- 修改：ym
     *参数：cinemaId
     *返回值:
     */
    public Result isCinema(@RequestParam("contractId")int contractId){
        //同意院线入住 发送短信消息 提醒
        //修改合同状态
        CinemaContract cinemaContract = cinemaContractMapper.selectById(contractId);
        cinemaContract.setState(3);
        cinemaContractMapper.updateById(cinemaContract);
        //修改影院状态
//        Integer cinemaId = cinemaContract.getCinemaId();
//        Cinema cinema = cinemaMapper.selectById(cinemaId);
//        String tel = cinema.getTel();
//        cinema.setState(2);
//        cinemaMapper.updateById(cinema);
        //==================================================发送短信通知

//        //调用短信接口
//        movieTicket.sendMessage(tel,massage);

        return Result.success("同意修改合同");
    }

    /**
     * 获取所有正在申请入驻的影院    -- 添加：ym
     */
    public Result getAllApplyForEnter(){
        List<Integer> cinemas=cinemaMapper.getAllApplyForEnter();
        if(cinemas.size()==0)
            return Result.fail(426,"没有正在申请的影院");
        return Result.success(cinemas);
    }

    /**
     * 同意影院入驻    -- 添加：ym
     */
    public Result configApply(int id){
        cinemaMapper.configApply(id);
        Cinema cinema = cinemaMapper.selectById(id);
        //申请成功，发送消息
        movieTicket.sendMessage(cinema.getTel(),"恭喜"+cinema.getLegalPerson()+"的"+cinema.getCinemaName()+"申请成功");
        return Result.success("同意申请");
    }

    /**
     *作者:木子
     *方法：不同意影院入住 ：审核合同 修改影院的状态为失败
     *参数：cinemaId
     *返回值:
     */
    public Result noCinema(@RequestParam("contractId")int contractId,@RequestParam("massage")String massage){
        //不同意院线入住 发送短信消息 提醒
        //修改状态
        CinemaContract cinemaContract = cinemaContractMapper.selectById(contractId);
        cinemaContract.setState(2);
        cinemaContractMapper.updateById(cinemaContract);

        //修改影院状态
        Integer cinemaId = cinemaContract.getCinemaId();
        Cinema cinema = cinemaMapper.selectById(cinemaId);
        String tel = cinema.getTel();
        cinema.setState(-1);
        cinemaMapper.updateById(cinema);
        //==================================================发送短信通知
        movieTicket.sendMessage(tel,massage);
        return Result.success();

    }
    /**
     *作者:木子
     *方法：关闭违规影院/院线
     *参数： cinemaId
     *返回值:
     */


    public Result deleteCinema(@RequestParam("cinemaId")int cinemaId,@RequestParam("massage")String massage){
       //关闭影院 包括院线 修改状态
        bossAcountService.deleteCinema(cinemaId);
        //发送短信通知 ======================================================
        String tel = cinemaMapper.selectById(cinemaId).getTel();
        movieTicket.sendMessage(tel,massage);
        return Result.success();
    }
    /**
     *作者:木子
     *方法：查询某个影院/院线合同
     *参数： cinemaId
     *返回值:
     */
    public Result getContracts(@RequestParam("cinemaId")int cinemaId,@RequestParam(defaultValue = "1")int currPage,@RequestParam(defaultValue = "5")int number){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("cinema_id",cinemaId);
        Page page = new Page(currPage,number);
        cinemaContractMapper.selectPage(page, wrapper);
        return Result.success(page);
    }

    /**
     *作者:木子
     *方法：查询正在申请影院/院线的合同
     *参数：
     *返回值:
     */
    public Result getIsCinemas(@RequestParam(defaultValue = "1")int currPage,@RequestParam(defaultValue = "5")int number){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("state",1);
        Page page = new Page(currPage,number);
        cinemaContractMapper.selectPage(page, wrapper);
        return Result.success(page);

    }

    /**
     *作者:木子
     *方法：查询某个用户的会员
     *参数：
     *返回值:
     */
    public Result getUserVip(@RequestParam("userId")int userId,@RequestParam(defaultValue = "1")int currPage,@RequestParam(defaultValue = "5")int number){
        //查看绑定 自定义返回类型 正常使用的
        Page page = new Page(currPage,number);
        IPage<VipPram> userVip = bossAcountMapper.findUserVip(page);
        return Result.success(userVip);
    }

    /**
     *作者:木子
     *方法：查询所有的优惠券活动
     *参数：
     *返回值:
     */
    public Result getDiscounts(@RequestParam(defaultValue = "1")int currPage,@RequestParam(defaultValue = "5")int number){
        Page page = new Page(currPage,number);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id",0);
        discountMapper.selectPage(page,wrapper);

        return Result.success(page);
    }

    /**
     *作者:木子
     *方法：查询某个用户的积分
     *参数：
     *返回值:
     */
    public Result getPoints(@RequestParam("userId")int userId){
        //平台查询 外部传userId
        User user = userMapper.selectById(userId);
        Integer points = user.getPoints();
        return Result.success(points);
    }

    /**
     *作者:木子
     *方法：查询某个用户的优惠券
     *参数：
     *返回值:
     */

    public Result getUserDiscounts(@RequestParam("userId")int userId,@RequestParam(defaultValue = "1")int currPage,@RequestParam(defaultValue = "5")int number){
        //平台查询 外部传userId
        Page page = new Page(currPage,number);
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("user_id",userId);
        discountMapper.selectPage(page,wrapper);
        return Result.success(page);
    }


    /**
     *作者:木子
     *方法：查询某个影院的详细信息
     *参数：
     *返回值:
     */
    public Result getOCinema(@RequestParam("cinemaId")int cinemaId){
        //详请
        List<?> cinema = bossAcountService.getCinema(cinemaId);

        return Result.success(cinema);
    }

    /**
     *作者:木子
     *方法：查询某个影院的投诉信息
     *参数：
     *返回值:
     */


    public Result getComplaints(@RequestParam("cinemaId")int cinemaId,@RequestParam(defaultValue = "1")int currPage,@RequestParam(defaultValue = "5")int number){
        Page page = new Page(currPage,number);
        IPage<ComplainPram> complains = bossAcountMapper.findComplain(page, cinemaId);
        return Result.success(complains);
    }

    /**
     *作者:木子
     *方法：查询某个用户的所有订单
     *参数：
     *返回值:
     */


    public Result getUserOrder(@RequestParam("userId")int userId,@RequestParam(defaultValue = "1")int currPage,@RequestParam(defaultValue = "5")int number){
        Page page = new Page(currPage,number);

        IPage<OrderMoviePram> movieOrders = bossAcountMapper.findMovieOrder(page, userId);

        return Result.success(movieOrders);
    }

    /**
     *作者:木子
     *方法：查询某个影院的所有订单
     *参数：
     *返回值:
     */


    public Result getCinemaOrder(@RequestParam("cinemaId")int cinemaId,@RequestParam(defaultValue = "1")int currPage,@RequestParam(defaultValue = "5")int number){
        Page page = new Page(currPage,number);
        IPage<OrderMoviePram> cinemaOrders = bossAcountMapper.findCinemaOrder(page, cinemaId);
        return Result.success(cinemaOrders);
    }

    /**
     *作者:木子
     *方法：设置添加优惠券活动
     *参数：
     *返回值:
     */


    public Result addDiscount(DiscountPram discountPram){
        Discount discount = new Discount();
        BeanUtils.copyProperties(discountPram,discount);
        discountMapper.insert(discount);
        return Result.success();
    }
    /**
     *作者:木子
     *方法：修改优惠券活动
     *参数：
     *返回值:
     */


    public Result changeDiscount(DiscountPram discountPram){
        Discount discount = new Discount();
        BeanUtils.copyProperties(discountPram,discount);
        discountMapper.updateById(discount);
        return Result.success();
    }

    /**
     *作者:木子
     *方法：删除某个优惠券活动
     *参数：
     *返回值:
     */


    public Result deleteDiscount( @RequestParam("discountId")int discountId){
       discountMapper.deleteById(discountId);
        return Result.success();
    }

    /**
     *作者:木子
     *方法：添加一个影片
     *参数：
     *返回值:
     */

    public Result addMovie(MoviePram moviePram){
        System.out.println(moviePram.toString());
        //前端传过来直接新增

        bossAcountMapper.addMovie(moviePram);
        return Result.success();
    }
    /**
     *作者:木子
     *方法：上传一个电影的文件 剧照 照片。。。
     *参数：
     *返回值:
     */
    public Result addMovieData(@Param("name")String name, @Param("type")String type, @Param("introduce") String introduce, @Param("time") Integer time, @Param("boxOffice") Double boxOffice, @Param("movieGrade") Double movieGrade, @Param("state") Integer state, @Param("releaseDate") String releaseDate, @Param("filePaths") MultipartFile[] filePaths, @Param("types") int types){
        MoviePram moviePram = new MoviePram();
        moviePram.setBoxOffice(boxOffice);
        moviePram.setFilePaths(filePaths);
        moviePram.setIntroduce(introduce);
        moviePram.setMovieGrade(movieGrade);
        moviePram.setName(name);
        moviePram.setReleaseDate(releaseDate);
        moviePram.setState(state);
        moviePram.setTime(time);
        moviePram.setType(type);
        moviePram.setTypes(types);
        //前端传过来直接新增
         bossAcountMapper.addMovie(moviePram);
        Integer movieId = moviePram.getId();
        System.out.println(movieId);
        //上传图片文件到服务器
        Result urls = updateUtil.updateFiles("movie", filePaths, movieId);
        String[] url = (String[])urls.getData();
        //新增
        //遍历 新增
        for (int index =0;index < url.length;index++){
            MovieData movieData = new MovieData();
            movieData.setType(types);
            movieData.setFilePath(url[index]);
            movieData.setMovieId(movieId);
            movieDataMapper.insert(movieData);
        }

        return Result.success();
    }
    /**
    *作者:木子
    *方法：已有电影 上传文件
    *参数：文件数组 电影id 文件数组
    *返回值:
    */
    public Result addMovieDatas(@RequestParam("movieId")Integer movieId,@RequestParam("type")int type,@Param("filePaths") MultipartFile[] filePaths){
        //上传图片文件到服务器
        Result urls = updateUtil.updateFiles("movie", filePaths, movieId);
        String[] url = (String[])urls.getData();
        for (int index =0;index < url.length;index++){
            MovieData movieData = new MovieData();
            movieData.setType(type);
            movieData.setFilePath(url[index]);
            movieData.setMovieId(movieId);
            movieDataMapper.insert(movieData);
        }

        return Result.success();
    }



    /**
     *作者:木子
     *方法：上传一个电影的文件 剧照 照片。。。
     *参数：
     *返回值:
     */

    public Result addPerson(@RequestParam("name") String name,@RequestParam("introduce")String introduce,@RequestParam("sex")String sex,@RequestParam("age")Integer age,@RequestParam("country")String country,@RequestParam("nation")String nation,@RequestParam("college")String college,@RequestParam("company")String company){
        Person person = new Person();
        person.setAge(age);
        person.setCollege(college);
        person.setCompany(company);
        person.setCountry(country);
        person.setIntroduce(introduce);
        person.setName(name);
        person.setNation(nation);
        person.setSex(sex);
        personMapper.insert(person);
        return Result.success();
    }
    /**
    *作者:木子
    *方法：根据明星名字查询出人物信息
    *参数：name  模糊查询
    *返回值:
    */
    public Result getPerson(@RequestParam("name") String name,@RequestParam(defaultValue = "1")int currPage,@RequestParam(defaultValue = "5")int number){
        Page<Person> page = new Page<>(currPage,number);
        IPage<Person> persons = bossAcountMapper.getPersons(page, name);
        return Result.success(persons);
    }


    /**
     *作者:木子
     *方法：上传人物照片。。。
     *参数：
     *返回值:
     */

    public Result addPersonData(@RequestParam("personId")int personId,@RequestParam("path")MultipartFile path){
        //================人物图片新增
        Result person = updateUtil.updateOneFile("person", path, personId);
        String data = (String)person.getData();
        PersonData personData = new PersonData();
        personData.setPersonId(personId);
        personData.setPath(data);
        //新增
        personDataMapper.insert(personData);

        return Result.success(personData);
    }

    /**
     *作者:木子
     *方法：添加影片人员照片对应关系/设置导演 演员
     *参数：
     *返回值:
     */
    public Result addMoviePerson(@RequestParam("movieId")int movieId,@RequestParam("personId")int personId,@RequestParam("type")int type){
        MoviePerson moviePerson = new MoviePerson();
        moviePerson.setMovieId(movieId);
        moviePerson.setPersonId(personId);
        moviePerson.setType(type);
        moviePersonMapper.insert(moviePerson);
        return Result.success();
    }



    /**
     *作者:木子
     *方法：修改一个影片
     *参数：
     *返回值:
     */

    public Result changeMovie(MoviePram moviePram){
        Movie movie = new Movie();
        BeanUtils.copyProperties(moviePram,movie);
        movieMapper.updateById(movie);
        return Result.success();
    }

    /**
     *作者:木子
     *方法：下架一个影片
     *参数：
     *返回值:
     */

    public Result deleteMovie(@RequestParam("movieId")int movieId){
        //下架影片 修改状态
        Movie movie = movieMapper.selectById(movieId);
        movie.setState(1);
        movieMapper.updateById(movie);
        return Result.success();
    }

    /**
    *作者:木子
    *方法：查询当月的盈利
    *参数：年月
    *返回值:
    */
    public Result getBoosAcount(@RequestParam("time") String time){
        QueryWrapper wrapper = new QueryWrapper();
        wrapper.eq("time",time);
        BossAcount bossAcount = bossAcountMapper.selectOne(wrapper);
        return Result.success(bossAcount);
    }

}

